import 'package:flutter/services.dart';
import 'package:flutter_hsh/utils/k_log.dart';

class HealthScore {
  static const MethodChannel _channel = MethodChannel('com.weather.quickly.report');

  /// 获取健康评分
  static Future<HealthScoreResult> getHealthScore() async {
    try {
      final Map<dynamic, dynamic> result =
      await _channel.invokeMethod('getHealthScore');

      return HealthScoreResult.fromMap(Map<String, dynamic>.from(result));
    } on PlatformException catch (e) {
      KLog.d('获取健康评分失败: ${e.message}');
      throw Exception('获取健康评分失败: ${e.message}');
    }
  }
}

/// 健康评分结果模型
class HealthScoreResult {
  final double overallScore;      // 总体健康评分
  final MemoryScore memory;       // 内存得分
  final StorageScore storage;     // 存储得分
  final TemperatureScore temperature; // 温度得分
  final SecurityScore security;   // 安全得分

  HealthScoreResult({
    required this.overallScore,
    required this.memory,
    required this.storage,
    required this.temperature,
    required this.security,
  });

  factory HealthScoreResult.fromMap(Map<String, dynamic> map) {
    return HealthScoreResult(
      overallScore: (map['overallScore'] ?? 0).toDouble(),
      memory: MemoryScore.fromMap(Map<String, dynamic>.from(map['memory'])),
      storage: StorageScore.fromMap(Map<String, dynamic>.from(map['storage'])),
      temperature: TemperatureScore.fromMap(Map<String, dynamic>.from(map['temperature'])),
      security: SecurityScore.fromMap(Map<String, dynamic>.from(map['security'])),
    );
  }
}

/// 内存得分
class MemoryScore {
  final double score;           // 内存得分 (0-100)
  final int totalMemory;        // 总内存 (字节)
  final int availableMemory;    // 可用内存 (字节)
  final double usagePercentage; // 内存使用率

  MemoryScore({
    required this.score,
    required this.totalMemory,
    required this.availableMemory,
    required this.usagePercentage,
  });

  factory MemoryScore.fromMap(Map<String, dynamic> map) {
    return MemoryScore(
      score: (map['score'] ?? 0).toDouble(),
      totalMemory: map['totalMemory'] ?? 0,
      availableMemory: map['availableMemory'] ?? 0,
      usagePercentage: (map['usagePercentage'] ?? 0).toDouble(),
    );
  }
}

/// 存储得分
class StorageScore {
  final double score;           // 存储得分 (0-100)
  final int totalStorage;       // 总存储 (字节)
  final int availableStorage;   // 可用存储 (字节)
  final double usagePercentage; // 存储使用率

  StorageScore({
    required this.score,
    required this.totalStorage,
    required this.availableStorage,
    required this.usagePercentage,
  });

  factory StorageScore.fromMap(Map<String, dynamic> map) {
    return StorageScore(
      score: (map['score'] ?? 0).toDouble(),
      totalStorage: map['totalStorage'] ?? 0,
      availableStorage: map['availableStorage'] ?? 0,
      usagePercentage: (map['usagePercentage'] ?? 0).toDouble(),
    );
  }
}

/// 温度得分
class TemperatureScore {
  final double score;           // 温度得分 (0-100)
  final double temperature;     // 当前温度 (°C)
  final String status;          // 状态: normal/high/overheat

  TemperatureScore({
    required this.score,
    required this.temperature,
    required this.status,
  });

  factory TemperatureScore.fromMap(Map<String, dynamic> map) {
    return TemperatureScore(
      score: (map['score'] ?? 0).toDouble(),
      temperature: (map['temperature'] ?? 0).toDouble(),
      status: map['status'] ?? 'normal',
    );
  }
}

/// 安全得分
class SecurityScore {
  final double score;           // 安全得分 (0-100)
  final String riskLevel;       // 风险等级: none/low/high
  final List<String> issues;    // 安全问题列表

  SecurityScore({
    required this.score,
    required this.riskLevel,
    required this.issues,
  });

  factory SecurityScore.fromMap(Map<String, dynamic> map) {
    return SecurityScore(
      score: (map['score'] ?? 0).toDouble(),
      riskLevel: map['riskLevel'] ?? 'none',
      issues: List<String>.from(map['issues'] ?? []),
    );
  }
}